<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Article;
use app\common\model\Version;
use think\Db;
use think\facade\Config;

/**
 * 首页接口.
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页.
     */
    public function index()
    {
        $this->success('请求成功');
    }

    public function test(){
        var_dump(strtotime('2022-08-08 24:00:00'));
        var_dump(strtotime('2022-08-09 00:00:00'));
    }

    public function common(){
        $data=[
            'phone'                 =>Config::get('site.phone'),
            'index_banner'          =>Config::get('site.index_banner')?:''
        ];

        foreach ($data['index_banner'] as &$i){
            $i=cdnurl($i,true);
        }

        $this->success('成功',$data);
    }

    public function guide(){
        $data=[
            'guide_video'   =>Config::get('site.guide_video'),
            'guide_images'  =>Config::get('site.guide_images'),
        ];

        $this->success('成功',$data);
    }
    /**
     *
     * 版本更新
     */
    public function version()
    {
        $os = request()->param('os','android');
        if(!in_array($os,['android','ios'])){
            $this->error('手机系统类型错误');
        }

        $version = Version::where(['os'=>$os])
            ->order('id desc')->find()?:[];

        $this->success('请求成功',$version);
    }

    /**
     * 文章信息
     */
    public function article()
    {
        $type=request()->param('type','user');
        $code=request()->param('code','');

        $article=Article::get(['code'=>$code,'type'=>'user']);

        if(!$article){
            $this->error('未找到对应信息');
        }

        $this->success('请求成功',$article);
    }


    /**
     *  获取配置信息
     */
    public function info()
    {
        $code=request()->param('code','');

        if(!in_array($code,['phone','index_banner'])){
            $this->error('此数据未开放获取');
        }

        $info=config('site.'.$code);

        if($code=='index_banner'){
            foreach ($info as &$i){
                $i=cdnurl($i,true);
            }
        }

        $this->success('请求成功',$info?:'');
    }

}
